﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using nl.tno.ict.multitouch.wpfmultitouch.events;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    /// <summary>
    /// Interaction logic for UCKeyboard.xaml
    /// </summary>
    public partial class UCKeyboard : UserControl
    {
        private Canvas _keyboardCanvas;
        private Key[] _characters = { Key.Q, Key.W, Key.E, Key.R, Key.T, Key.Y, Key.U, Key.I, Key.O, Key.P, Key.Back,
                                    Key.LeftShift, Key.A, Key.S, Key.D, Key.F, Key.G, Key.H, Key.J, Key.K, Key.L, Key.Enter,
                                    Key.Z, Key.X, Key.C, Key.V, Key.B, Key.N, Key.M, Key.OemPeriod, Key.D1, Key.Oem2, Key.Escape,
                                    Key.Space };
        public event ControlKeyboardButtonHandler keyboardHandler;

        private int _keysize = 60;
        public int KeySize 
        { 
            get 
            { 
                return _keysize; 
            } 
            set 
            { 
                _keysize = value;
                if (_keysize < 30)
                    _keysize = 30;
                if (_keysize > 80)
                    _keysize = 80;
                _keyboardCanvas.Height = 4 * _keysize;
                _keyboardCanvas.Width = 11 * _keysize;
                for (int i = 0; i < _keyboardCanvas.Children.Count; i++ )
                {
                    if (((MTButton)_keyboardCanvas.Children[i]).Content.ToString() == "Space")
                    {
                        ((MTButton)_keyboardCanvas.Children[i]).Height = _keysize;
                        ((MTButton)_keyboardCanvas.Children[i]).Width = _keysize * 11;
                    }
                    else
                    {
                        ((MTButton)_keyboardCanvas.Children[i]).Height = _keysize;
                        ((MTButton)_keyboardCanvas.Children[i]).Width = _keysize;
                    }
                    this.setKeyPositions(i);
                    if (_keysize >= 30 && _keysize < 50)
                        ((MTButton)_keyboardCanvas.Children[i]).FontSize = 10;
                    else
                        ((MTButton)_keyboardCanvas.Children[i]).FontSize = 16;
                }
            } 
        }

        public int GetKeyboardHeight { get { return 4 * _keysize; } }

        public UCKeyboard()
        {
            InitializeComponent();

            _keyboardCanvas = new Canvas();
            _keyboardCanvas.Height = 4 * _keysize;
            _keyboardCanvas.Width = 11 * _keysize;
            _keyboardCanvas.Background = new SolidColorBrush(Colors.Black);            
            
            this.AddChild(_keyboardCanvas);
            
            this.createKeys();
        }

        private void createKeys()
        {
            for (int i = 0; i < _characters.Length; i++)
            {
                MTButton mtButton = new MTButton();
                mtButton.Tag = _characters[i];
                mtButton.CanBeClicked = true;
                mtButton.CanBeDragged = false;
                mtButton.CanBeScaled = false;

                switch ((Key)_characters.GetValue(i))
                {
                    case Key.Back:
                        mtButton.Content = "Back\rSpace";
                        break;
                    case Key.LeftShift:
                        mtButton.Content = "Shift";
                        break;
                    case Key.Enter:
                        mtButton.Content = "Enter";
                        break;
                    case Key.OemPeriod:
                        mtButton.Content = ".";
                        break;
                    case Key.D1:
                        mtButton.Content = "!";
                        break;
                    case Key.Oem2:
                        mtButton.Content = "?";
                        break;
                    case Key.Escape:
                        mtButton.Content = "Close";
                        break;
                    default:
                        mtButton.Content = ((Key)_characters.GetValue(i)).ToString();
                        break;
                }
                
                mtButton.FontSize = 16;
                if (mtButton.Content.ToString() == "Space")
                {
                    mtButton.Height = _keysize;
                    mtButton.Width = _keysize * 11;
                }
                else
                {
                    mtButton.Height = _keysize;
                    mtButton.Width = _keysize;
                }                
                _keyboardCanvas.Children.Add(mtButton);
                mtButton.Click += new RoutedEventHandler(mtButton_Click);                
                this.setKeyPositions(i);
            }
        }        

        private void setKeyPositions(int n)
        {
            if (n < 11)
            {
                MTCanvas.SetLeft(_keyboardCanvas.Children[n], n * _keysize);
                MTCanvas.SetTop(_keyboardCanvas.Children[n], 0);
            }
            else if (n >= 11 && n <= 21)
            {
                MTCanvas.SetLeft(_keyboardCanvas.Children[n], (n - 11) * _keysize);
                MTCanvas.SetTop(_keyboardCanvas.Children[n], _keysize);
            }
            else if (n >= 22 && n <= 32)
            {
                MTCanvas.SetLeft(_keyboardCanvas.Children[n], (n - 22) * _keysize);
                MTCanvas.SetTop(_keyboardCanvas.Children[n], 2 * _keysize);
            }
            else
            {
                MTCanvas.SetLeft(_keyboardCanvas.Children[n], 0);
                MTCanvas.SetTop(_keyboardCanvas.Children[n], 3 * _keysize);
            }
        }

        public void mtButton_Click(object sender, RoutedEventArgs e)
        {
            if (keyboardHandler != null) keyboardHandler(this, new ControlKeyboardEventArgs((Key)((MTButton)e.Source).Tag));
        }
    }
}
